﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using Kugar.Core.ExtMethod;
using Kugar.UI.Web.AdminLTE.Helpers;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace Kugar.UI.Web.AdminLTE.PageControls.PageForms
{
    public class PageForm_CheckedListField : PageForm_SingleInputFieldBase
    {
        private string[] _checkedValues = null;
        private SelectorItem[] _options = null;
        private bool _disabled = false;
        //private string _name = "";

        public PageForm_CheckedListField(PageForm_Form form, ViewContext viewContext) : base(form, viewContext)
        {
            this.Css("webui-form_checklistfield");
        }

        //public PageForm_CheckedListField Name(string name)
        //{
        //    _name = name;

        //    return this;
        //}

        public PageForm_CheckedListField Checked(params string[] checkedValues)
        {
            _checkedValues = checkedValues;

            return this;
        }

        public PageForm_CheckedListField Checked(params int[] checkedValues)
        {
            _checkedValues= checkedValues.Select(x => x.ToString()).ToArrayEx();

            return this;
        }

        public PageForm_CheckedListField Options(IEnumerable<SelectorItem> options)
        {
            _options = options.ToArrayEx();

            return this;
        }

        public PageForm_CheckedListField Options(params (string title, string value)[] options)
        {
            _options = options.Select(x => new SelectorItem(x.title, x.value)).ToArrayEx();

            return this;
        }

        public PageForm_CheckedListField Options<TData>(IEnumerable<TData> values, Func<TData, string> titleGetter,
            Func<TData, string> valueGetter, Func<TData, bool> disabledStateGetter = null)
        {
            _options = values?.Select(x => new SelectorItem(titleGetter(x), valueGetter(x), disabledStateGetter?.Invoke(x)??false))
                .ToArrayEx();

            return this;
        }

        /// <summary>
        /// 选项列表,根据枚举类型
        /// </summary>
        /// <param name="enumType"></param>
        /// <returns></returns>
        public PageForm_CheckedListField Options(Type enumType, Func<SelectorItem, bool> disabledGetter = null)
        {
            if (!enumType.IsEnum)
            {
                throw new ArrayTypeMismatchException("enumType参数类型必须为枚举类型");
            }

            //var names = enumType.GetEnumNames();
            //var values = (int[])enumType.GetEnumValues();

            //var lst = new List<SelectorItem>(names.Length);

            //for (int i = 0; i < names.Length; i++)
            //{
            //    FieldInfo enumInfo = enumType.GetField(names[i]);

            //    var a = (DescriptionAttribute)enumInfo.GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault();

            //    var k = new SelectorItem(a != null ? a.Description : names[i].ToStringEx(), values[i].ToString(), disabledGetter?.Invoke(values[i].ToString()) ?? false);

            //    lst.Add(k);
            //}

            //_options = lst.ToArrayEx();

            _options = Utility.GetEnumTypeToSelectorItems(enumType);

            if (disabledGetter != null)
            {
                foreach (var opt in _options)
                {
                    opt.Disabled = disabledGetter(opt);
                }
            }

            return this;
        }

        public PageForm_CheckedListField Disabled(bool disabled = true)
        {
            _disabled = disabled;

            return this;
        }

        protected override void RenderContent(TextWriter writer)
        {
            if (_options!=null)
            {
                foreach (var option in _options)
                {
                    writer.WriteLine($"<input type=\"checkbox\" {_checkedValues?.Contains(option.Value).If("checked")} name=\"{_name}\" title=\"{option.Title}\" value=\"{option.Value}\" {(_disabled || option.Disabled).If("disabled")} lay-skin=\"primary\">");
                }

            }
            //foreach (var option in _options)
            //{
            //    writer.WriteLine($"<input type=\"checkbox\" {_checkedValues?.Contains(option.Value).If("checked")} name=\"{_name}\" title=\"{option.Title}\" value=\"{option.Value}\" {(_disabled || option.Disabled).If("disabled")} lay-skin=\"primary\">");
            //}
        }
    }
}